package perso.perso.wsclient;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import perso.perso.view.admin.user.BeanUser;
import perso.perso.view.admin.user.BeanUserList;
import perso.perso.view.tools.HttpTools;
import perso.perso.view.tools.PersoViewConstant;

import com.thoughtworks.xstream.XStream;

public class HttpMiddlewareUserWSCall extends HttpMiddleware {

	public static String URI_GET_ALL_USER =  "/easySchoolWS/middleware/user/getAllUsers";
	public static String URI_GET_ALL_USER_BY_FILTER =  "/easySchoolWS/middleware/user/getAllUsersByFilter";
	public static String URI_ADD_USER =  "/easySchoolWS/middleware/user/addUser";
	public static String URI_UPDATE_USER =  "/easySchoolWS/middleware/user/updateUser";
	public static String URI_REMOVE_USER =  "/easySchoolWS/middleware/user/removeUser";
	public static String URI_ADD_TEACHER_CLASS =  "/easySchoolWS/middleware/user/addTeachClasses";
	public static String URI_REMOVE_TEACHER_CLASS =  "/easySchoolWS/middleware/user/removeTeachClasses";


	public List<BeanUser> getAllUsers() throws ClientProtocolException, IOException{



		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_GET_ALL_USER);


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanUserList.class);

		List<BeanUser> list = ((BeanUserList)xstream.fromXML(xmlContent)).getList();
//		if(list != null){
//			HttpMiddlewareSchoolWSCall h = new HttpMiddlewareSchoolWSCall();
//			for (BeanUser beanUser : list) {
//				beanUser.setTheClass(h.getSchoolClassById(beanUser.getIdClass()));
//				beanUser.getTheClass().setSchool(h.getSchoolById(beanUser.getTheClass().getSchoolId()));
//			}
//		}


		return list;
	}

	public List<BeanUser> getAllUsersByFilters(String name, String surname, Date birthday, boolean onlyProfessor) throws ClientProtocolException, IOException{

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_GET_ALL_USER_BY_FILTER
				+"?name="+name.replace(" ", "%20")+"&surname="+surname.replace(" ", "%20")+"&birthday="+sdf.format(birthday).replace(" ", "%20")+"&isProfessor="+(onlyProfessor?PersoViewConstant.YES:PersoViewConstant.NO));


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanUserList.class);

		BeanUserList beanUserList = (BeanUserList)xstream.fromXML(xmlContent);

		return beanUserList.getList();
	}

	public BeanUser addUser(BeanUser user) throws ClientProtocolException, IOException{

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_ADD_USER);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.add(new BasicNameValuePair("name", user.getName()));
		nameValuePairs.add(new BasicNameValuePair("surname", user.getSurname()));

		nameValuePairs.add(new BasicNameValuePair("idClass", user.getIdClass().toString()));

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		nameValuePairs.add(new BasicNameValuePair("birthday", sdf.format(user.getBirthday())));
		
		nameValuePairs.add(new BasicNameValuePair("isProfessor", user.getIsProfessor()?PersoViewConstant.YES:PersoViewConstant.NO));

		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

		HttpResponse response = httpclient.execute(httppost);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanUser.class);

		return (BeanUser)xstream.fromXML(xmlContent);

	}
	
	public BeanUser updateUser(BeanUser user) throws ClientProtocolException, IOException{

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_UPDATE_USER);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		
		nameValuePairs.add(new BasicNameValuePair("idUser", user.getId()+""));
		nameValuePairs.add(new BasicNameValuePair("name", user.getName()));
		nameValuePairs.add(new BasicNameValuePair("surname", user.getSurname()));

		nameValuePairs.add(new BasicNameValuePair("idClass", user.getIdClass().toString()));

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		nameValuePairs.add(new BasicNameValuePair("birthday", sdf.format(user.getBirthday())));

		nameValuePairs.add(new BasicNameValuePair("isProfessor", user.getIsProfessor()?PersoViewConstant.YES:PersoViewConstant.NO));
		
		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

		HttpResponse response = httpclient.execute(httppost);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanUser.class);

		return (BeanUser)xstream.fromXML(xmlContent);

	}
	




	//  List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
	//  nameValuePairs.add(new BasicNameValuePair("login",
	//          username));
	//  nameValuePairs.add(new BasicNameValuePair("password",
	//          password));
	//  httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	public boolean removeUser(Integer id) throws ClientProtocolException, IOException{



		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_REMOVE_USER
				+"?id=" + id);


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());


		return true;
	}
	
	public boolean removeClassesUser(Integer idClass, Integer idUser) throws ClientProtocolException, IOException{



		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_REMOVE_TEACHER_CLASS
				+"?idClass=" + idClass+"&idUser="+idUser);


		HttpResponse response = httpclient.execute(httpget);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());


		return true;
	}

	
	
	public BeanUser addTeacherClass(String idUser,String idClass) throws ClientProtocolException, IOException{

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost("http://" 
				+ properties.getMiddlewareUrl() 
				+ ":"
				+ properties.getMiddlewarePort() 
				+ URI_ADD_TEACHER_CLASS);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.add(new BasicNameValuePair("idUser", idUser));
		nameValuePairs.add(new BasicNameValuePair("idClass", idClass));
		
		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

		HttpResponse response = httpclient.execute(httppost);
		String xmlContent = HttpTools.getStringContent(response.getEntity().getContent());

		XStream xstream = new XStream();  
		xstream.processAnnotations(BeanUser.class);

		return (BeanUser)xstream.fromXML(xmlContent);

	}
}
